package com.training.schedule_management_system_admin.service;

import cn.hutool.core.date.DateUtil;
import com.training.schedule_management_system_admin.entity.Schedule;
import com.training.schedule_management_system_admin.entity.ScheduleExample;
import com.training.schedule_management_system_admin.entity.VO.ScheduleVO;
import com.training.schedule_management_system_admin.mapper.ScheduleMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class ScheduleServce {

    @Autowired
    ScheduleMapper scheduleMapper;


    /*
    某日程表下的所有日程
     */
    public List<Schedule> getAllByTableId(Integer scheduletableId) {
        ScheduleExample example=new ScheduleExample();
        ScheduleExample.Criteria criteria = example.createCriteria();
        criteria.andScheduletableIdEqualTo(scheduletableId);
        example.setOrderByClause("date asc,create_time asc");
        List<Schedule> schedules = scheduleMapper.selectByExample(example);
        if(schedules!=null && !schedules.isEmpty()){
            return schedules;
        }else{
            return Collections.emptyList();
        }
    }

    /*
    保存日程
     */
    public int save(ScheduleVO scheduleVO,Schedule schedule) {

        BeanUtils.copyProperties(scheduleVO,schedule);
        if(!scheduleVO.getDate().equals("") && scheduleVO.getDate()!=null){
            schedule.setDate(DateUtil.parse(scheduleVO.getDate(),"yyyy-MM-dd"));
        }
        if(!scheduleVO.getCreateTime().equals("") && scheduleVO.getCreateTime()!=null) {
            schedule.setCreateTime(DateUtil.parse(scheduleVO.getCreateTime(), "HH:mm"));
        }
        if(!scheduleVO.getEndTime().equals("") && scheduleVO.getEndTime()!=null) {
            schedule.setEndTime(DateUtil.parse(scheduleVO.getEndTime(), "HH:mm"));
        }
        if(!scheduleVO.getOrderNum().equals("") && scheduleVO.getOrderNum()!=null) {
            schedule.setOrderNum(Integer.parseInt(scheduleVO.getOrderNum()));
        }

        return scheduleMapper.insertSelective(schedule);
    }

    /*
    通过id查找日程
     */
    public Schedule getById(Integer id) {
        return scheduleMapper.selectByPrimaryKey(id);
    }

    /*
    通过id删除日程
     */
    public int deleteById(Integer id) {
        return scheduleMapper.deleteByPrimaryKey(id);
    }

    /*
    批量删除日程
     */
    public int deleteBatch(List<Integer> ids) {
        ScheduleExample example=new ScheduleExample();
        ScheduleExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(ids);
        return scheduleMapper.deleteByExample(example);
    }

    /*
    删除某一日程表上的所有日程
     */
    public int delete(Integer scheduleId) {
        ScheduleExample example=new ScheduleExample();
        ScheduleExample.Criteria criteria = example.createCriteria();
        criteria.andScheduletableIdEqualTo(scheduleId);
        return scheduleMapper.deleteByExample(example);
    }

    /*
    批量添加日程
     */
    public int saveList(List<Schedule> schedules) {
        return scheduleMapper.insertList(schedules);
    }
}
